package com.lngsyz.file.biz.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lngsyz.file.biz.service.BizFileService;
import com.lngsyz.file.biz.service.TransFileService;
import com.lngsyz.file.util.StringUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
@Api(tags = "BizFileController", description = "业务接口")
@RequestMapping("v1/biz/file")
public class BizFileController {
	
	@Autowired
	BizFileService bizFileService;
	@Autowired
	TransFileService transFileService;

	// 上传文件
//	@AopLog(operate = "文件上传")
//	@ApiOperation(value = "上传文件", notes = "上传文件")
//	@RequestMapping(value = "/upload", method = RequestMethod.POST)
//	@ResponseBody
//	public URIResult<String> upload(@RequestParam("file") MultipartFile multipartFile) {
//		try {
//			String data = bizFileService.upload(multipartFile);
//			return new URIResult<String>(CommonConstant.CODE_SUCCESS, "", data);
//		} catch (Exception e) {
//			e.printStackTrace();
//			return new URIResult<String>(CommonConstant.CODE_FAILURE, e.getMessage());
//		}
//	}
//	
//	@AopLog(operate = "按指定路径，文件上传")
//	@ApiOperation(value = "按指定路径，上传文件", notes = "按指定路径，上传文件")
//	@RequestMapping(value = "/uploadWithPath", method = RequestMethod.POST)
//	@ResponseBody
//	public void uploadWithPath(@RequestParam("file") MultipartFile multipartFile,
//			@RequestParam("path") String path,
//			HttpServletResponse response) {
//		try {
//			bizFileService.uploadWithPath(multipartFile, path);
//		} catch (Exception e) {
//			StringUtil.returnJsonErr0(response, e.getMessage());
//		}
//	}

	// 获取文件流
	@ApiOperation(value = "获取文件流（根据相对路径）", notes = "获取文件流（根据相对路径）")
	@RequestMapping(value = "/getFileStreamByAccessUrl", method = RequestMethod.GET)
	@ResponseBody
	public void getFileStreamByAccessUrl(
			@ApiParam(name = "accessUrl", value = "文件相对路径（/img/***/a.png)")@RequestParam("accessUrl") String accessUrl,
			@ApiParam(name = "fileName", value = "文件名（aaa.png）")@RequestParam("fileName") String fileName,
			HttpServletResponse response) {
		try {
			bizFileService.getFileStreamByAccessUrl(accessUrl, fileName, response);
		} catch (Exception e) {
			System.out.println(fileName + "(" + accessUrl + ")文件读取异常");
			e.printStackTrace();
		}
	}
	
	// 获取文件流
//	@ApiOperation(value = "获取文件流", notes = "获取文件流")
//	@RequestMapping(value = "/getFileStream", method = RequestMethod.GET)
//	@ResponseBody
//	public void getFileStream(@ApiParam(name = "id", value = "文件ID")@RequestParam("id") String id,
//			HttpServletResponse response) {
//		try {
//			bizFileService.getFileStream(id, response);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
	// 按日期执行同步
//	@ApiOperation(value = "按日期执行同步", notes = "按日期执行同步")
//	@RequestMapping(value = "/transFileJob", method = RequestMethod.GET)
//	@ResponseBody
//	public void transFileJob(@ApiParam(name = "startDate", value = "起始日期yyyy-mm-dd") @RequestParam("startDate") String startDate,
//			@ApiParam(name = "days", value = "持续天数") @RequestParam("days") int days,
//			HttpServletResponse response) {
//		try {
//			bizFileService.transFileJob(startDate, days);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
	@ApiOperation(value = "获取服务器剩余存储空间", notes = "获取服务器剩余存储空间")
	@RequestMapping(value = "/getDiskFree", method = RequestMethod.GET)
	@ResponseBody
	public void getDiskFree(@ApiParam(name = "path", value = "查询路径（/data）") @RequestParam("path") String path,
			HttpServletResponse response) {
		try {
			String rtnStr = transFileService.checkDiskSpace(path);
			StringUtil.returnJson(response, rtnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 按日期执行同步
	// 该删除：2021-11-25
//	@ApiOperation(value = "按日期执行删除", notes = "按日期执行删除")
//	@RequestMapping(value = "/delFileJob", method = RequestMethod.GET)
//	@ResponseBody
//	public void delFileJob(@ApiParam(name = "startDate", value = "起始日期yyyy-mm-dd") @RequestParam("startDate") String startDate,
//			@ApiParam(name = "days", value = "持续天数") @RequestParam("days") int days,
//			HttpServletResponse response) {
//		try {
//			bizFileService.delFileJob(startDate, days);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
}